package com.atguigu.gmall.common.util;

import com.atguigu.gmall.model.to.UserAuthTo;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/10/5 7:58
 */
public class UserAuthToUtil {

    public static UserAuthTo getUserAuthTo(){
        //底层用的ThreadLocal原理,请求链是同一线程,创建一个map,key为同一线程
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId = request.getHeader("UserId");
        String userTempId = request.getHeader("UserTempId");
        UserAuthTo userAuthTo = new UserAuthTo();
        if (!StringUtils.isEmpty(userId)){
            userAuthTo.setUserId(Long.parseLong(userId));
        }
        if (!StringUtils.isEmpty(userTempId)){
            userAuthTo.setUserTempId(userTempId);
        }
        return userAuthTo;
    }
}
